﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;

namespace Temp.Minigame.MiniGame2
{
    class Game2
    {
        ContentManager content;
        AlexGame alexGame;
        private GraphicsDeviceManager _graphics;
        private Texture2D bg, continueButton, textBox, peer;
        private Rectangle mouseBox, continueBox;
        private MouseState mouse, mousePast;
        private Rectangle[] cArea;
        private Texture2D[] colors;
        private int moves, watch;
        private List<int> sequence;
        private DispatcherTimer timer, idleTimer;
        private Random ran;
        private int i, curColor, stopWatch, turn, win;
        private bool idle, directions;


        public Game2(ContentManager content, AlexGame alexGame)
        {
            this.content = content;
            this.alexGame = alexGame;
        }

        public void Initialize()
        {
            directions = false;
            continueBox = new Rectangle(975, 700, 153, 56);
            cArea = new Rectangle[4] { new Rectangle(378, 84, 300, 306), new Rectangle(678, 84, 300, 306), new Rectangle(378, 399, 300, 306), new Rectangle(678, 399, 300, 306) };
            colors = new Texture2D[4];
            mouseBox = new Rectangle(0, 0, 30, 30);
            sequence = new List<int>();
            ran = new Random();
            moves = 1;
            win = 0;
            turn = 0;
            curColor = -1;

            idle = true;


        }
        public void game()
        {
                stopWatch = moves;
                timer = new DispatcherTimer();
                timer.Tick += random;
                timer.Interval = new TimeSpan(0, 0, 0, 0, 750);
                timer.Start();
                i = 0;
        }

        private void random(object sender, object e)
        {
            if (i == stopWatch)
            {
                timer.Stop();
                curColor = -1;
                turn = 1;
                i = 0;
            }
            else
            {
                sequence.Add(ran.Next(4));
                if (i < 1)
                    flashColor(sequence[i], -1);
                else
                    flashColor(sequence[i], sequence[i - 1]);
                i++;
            }

        }

        private void playerMove()
        {
            if (mouse.LeftButton == ButtonState.Pressed)
            {
                if (mouseBox.Intersects(cArea[0]))
                    curColor = 0;
                else if (mouseBox.Intersects(cArea[1]))
                    curColor = 1;
                else if (mouseBox.Intersects(cArea[2]))
                    curColor = 2;
                else if (mouseBox.Intersects(cArea[3]))
                    curColor = 3;
            }
            else if (mouse.LeftButton == ButtonState.Released && mousePast.LeftButton == ButtonState.Pressed)
            {
                curColor = -1;
                if (mouseBox.Intersects(cArea[0]))
                    checkSequence(0);
                else if (mouseBox.Intersects(cArea[1]))
                    checkSequence(1);
                else if (mouseBox.Intersects(cArea[2]))
                    checkSequence(2);
                else if (mouseBox.Intersects(cArea[3]))
                    checkSequence(3);
            }
        }

        private void checkSequence(int selected)
        {
            System.Diagnostics.Debug.WriteLine("Moves: " + moves + " i: " + i + " sequence[i]: " + sequence[i] + " selected: " + selected);

            if (selected == sequence[i] && i < moves - 1)
            {
                i++;
            }
            else if (selected != sequence[i])
            {
                win = -1;
                ctr = 1000;
            }
            else
            {
                moves++;
                turn = 0;
                game();
            }
        }

        private void flashColor(int color, int lastColor)
        {
            if (color == lastColor)
            {
                idle = false;
                idleTimer = new DispatcherTimer();
                idleTimer.Tick += idleTimeCheck;
                idleTimer.Interval = new TimeSpan(0, 0, 0, 0, 50);
                idleTimer.Start();
            }
            curColor = color;
        }

        private void idleTimeCheck(object sender, object e)
        {
            idle = true;
            idleTimer.Stop();

        }

        public void LoadContent()
        {
            bg = content.Load<Texture2D>("Game2/Simon");
            continueButton = content.Load<Texture2D>("UI elements/continue");
            textBox = content.Load<Texture2D>("UI elements/storyBox");
            
            peer = content.Load<Texture2D>("Peer/Boy/peer.png");
            
            colors[0] = content.Load<Texture2D>("Game2/green");
            colors[1] = content.Load<Texture2D>("Game2/red");
            colors[2] = content.Load<Texture2D>("Game2/blue");
            colors[3] = content.Load<Texture2D>("Game2/yellow");

        }
        int ctr = 0;

        public void Update()
        {
            if (ctr != 1000)
            {
                mousePast = mouse;
                mouse = Mouse.GetState();
                mouseBox.X = mouse.X;
                mouseBox.Y = mouse.Y;
                checkDirections();
                if (turn > 0)
                    playerMove();

                //ctr++;

            }
            else
            {
                alexGame.inMinigame2 = false;
                alexGame.drawAlexWorld = true;

            }

        }

        private void checkDirections()
        {
            if (mouseBox.Intersects(continueBox) && mousePast.LeftButton == ButtonState.Pressed && mouse.LeftButton == ButtonState.Released)
            {
                directions = true;
                game();
            }
        }

        public void Draw(SpriteBatch _spriteBatch)
        {
            _spriteBatch.Draw(bg, new Vector2(0, 0), Color.White);
            if (directions == false)
            {
                _spriteBatch.Draw(textBox, new Vector2(255, 520), Color.White);
                _spriteBatch.Draw(peer, new Vector2(15, 500), Color.White);
                _spriteBatch.DrawString(content.Load<SpriteFont>("Game3/Fonty"), "To play Simon, copy the flashing pattern by clicking on the same colors in order as they were \nflashed.", new Vector2(325, 555), Color.Black);
                _spriteBatch.Draw(continueButton, new Vector2(975, 700), Color.White);
            }
            if (curColor >= 0 && idle == true)
                _spriteBatch.Draw(colors[curColor], new Vector2(0, 0), Color.White);
            //if (win == -1)
            //_spriteBatch.DrawString(content.Load<SpriteFont>("Game2/Fonty"), "Very good!\n Your score is " + moves + "!", new Vector2(500, 20), Color.Black);

        }

    }
}
